package javax.xianfeng.drools.hello;

import java.io.IOException;

import javax.xianfeng.drools.config.DroolsConfig;
import javax.xianfeng.drools.util.DrlUtils;
import javax.xianfeng.drools.util.DroolsUtils;

import org.junit.Test;
import org.kie.api.runtime.KieSession;
import org.kie.internal.utils.KieHelper;

import lombok.extern.slf4j.Slf4j;

/**
 * @since 2020/04/07 21:25:13
 */
@Slf4j
public class HelloTest3 {

	/**
	 * 执行drl字符串内容（文件读取）
	 * 
	 * @throws IOException
	 * @since 2020/04/07 21:47:10
	 */
	@Test
	public void hello() throws IOException {
		// 读取drl文件内容，也可从db读取
		String filePath = DroolsConfig.RULE_ROOT_PATH + "/hello/helloworld.drl";
		String ruleContent = DrlUtils.readDrlFile(filePath);

		KieHelper kieHelper = DroolsUtils.getKieHelper(ruleContent);
		KieSession kieSession = DroolsUtils.createSession(kieHelper);
		try {
			kieSession.fireAllRules();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			DroolsUtils.closeSession(kieSession);
		}

	}

}
